#Random Forest and Table A4

load("tree_dat.Rdata")
set.seed(1015)
rf_grid <- expand.grid(mtry = c(2,3,4),
                       splitrule = c("extratrees","gini"),
                       min.node.size = c(1,2,3,4,5))
rf_grid
fit_control<-trainControl(method = "repeatedcv", repeats = 5,
                          classProbs = TRUE,
                          summaryFunction = multiClassSummary)
rf_fit <- train(as.factor(Code) ~ ., 
                data = tree_dat,
                preProc = c("center", "scale"),
                method = "ranger",
                trControl=fit_control,
                tuneGrid=rf_grid)
rf_fit
plot(rf_fit)
classify<-cbind.data.frame(predict(rf_fit), tree_dat$Code)
names(classify)<-c("predicted", "Code")

kripp.alpha(t(classify))
tab<-table(classify)
prop.table(tab, margin=2)
confusionMatrix(tab)


#Calculate Class Level ROC
ROC<-cbind.data.frame(predict(rf_fit, type="prob"), tree_dat$Code)
ROC[,5]<-as.numeric(ROC[,5])-1
null<-cbind.data.frame(ROC[,1], ifelse(ROC[,5]==0,1,0))
roc(response=null[,2], predictor =null[,1])
plot.roc(null[,2], null[,1])

null<-cbind.data.frame(ROC[,2], ifelse(ROC[,5]==1,1,0))
roc(null[,2], null[,1])
plot.roc(null[,2], null[,1])

null<-cbind.data.frame(ROC[,3], ifelse(ROC[,5]==2,1,0))
roc(null[,2], null[,1])
plot.roc(null[,2], null[,1])

null<-cbind.data.frame(ROC[,4], ifelse(ROC[,5]==3,1,0))
roc(null[,2], null[,1])
plot.roc(null[,2], null[,1])

rm(list=ls())
